﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace DDD {
    /// <summary>
    /// <see cref="VertexShader"/>クラスに拡張メソッドを追加するクラス
    /// </summary>
    public static class VertexShaderExtensions {
        /// <summary>
        /// コンパイルします
        /// </summary>
        /// <param name="vs"><see cref="VertexShader"/>オブジェクト</param>
        public static void Compile (this VertexShader vs) {
            var cache = Graphics3DExtensions.GetGLCache ();
            var id = cache[vs];
            if (id > 0) {
                return;
            }

            id = GL.CreateShader (ShaderType.VertexShader);
            if (id == 0) {
                throw new InvalidOperationException ("glCreateShader was failed");
            }

            GL.ShaderSource (id, vs.Source);
            GL.CompileShader (id);

            vs.SetLog (GL.GetShaderInfoLog (id));

            int status;
            GL.GetShader (id, ShaderParameter.CompileStatus, out status);
            if (status == 0) {
                throw new InvalidOperationException ("Compile of VertexShader was Failed, msg=" + vs.CompileLog);
            }

            cache.Add (vs, id);
        }
    }
}
